﻿// testarray.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
using namespace std;
int main()
{
    //栈区数组
    {
        //类型 变量名[数组数量]
        int arr1[4]{ 0 };

        //c++11 for循环遍历
        // for (int a : arr1)
           // cout << a << " ";
        // cout << endl;

        arr1[0] = 99;
        arr1[2] = 88;
        //for (int a : arr1)
            //cout << a << " ";
        //cout << endl;

        //cout << "sizeof(arr1) = " << sizeof(arr1) << endl;
        //for (int i = 0; i < sizeof(arr1) / sizeof(int); i++)
            //cout << arr1[i] << ",";
        //cout << endl;

        //栈区数组空间只能用编译时常量
        //int size = 5;
        //int arr3[size];
        //const int csize = size;
        //int arr3[csize];
        constexpr int cesize = 5;
        int arr4[cesize];

        //数组初始化
        int arr5[10] = { 1,2,3,4,5 };
        int arr6[] = { 3,4,5,6,7,8 };
        char arr7[] = "test string";
        //cout << "sizeof(arr6) = " << sizeof(arr6) << endl;
        //cout << "sizeof(arr7) = " << sizeof(arr7) << endl;

        // 数组是地址连续的空间
        //输出数组的首地址
        //cout << "arr1 = " << (long long)arr1 << endl;
        //cout << "&arr1[0]" << (long long)&arr1[0] << endl;
        //cout << "&arr1[1]" << (long long)&arr1[1] << endl;
        //cout << "&arr1[2]" << (long long)&arr1[2] << endl;
        
        //cout << "arr1[2] = " << arr1[2] << endl;
        //cout << "*(arr1 + 2) = " << *(arr1 + 2) << endl;


        int arr2[4]; //未初始化的数组，内容不确定
        //for (int a : arr2)
            //cout << a << " ";
        //cout << endl;

    }
    {
        int arr1[4]{ 0 };
    }
    {
        //堆区数组
        int* arr1 = new int[4] {0};
        auto arr2 = new int[4] {1, 2, 3, 4};
        //cout << "sizeof(arr1) = " << sizeof(arr1) << endl;
        int size = 10;
        auto arr3 = new int[size] {0};
        //for (int i = 0; i < size; i++)
        //{
            //cout << arr3[i] << "|";
        //}
        //cout << endl;
        delete[] arr1; arr1 = nullptr;
        delete[] arr2; arr2 = nullptr;
        delete[] arr3; arr3 = nullptr;

        int arr5[5] = { 1,2,3,4,5 };
        auto* arr6 = new int[5];
        memcpy(arr6, arr5, sizeof(arr5));
        for (int i = 0; i < 5; i++)
            cout << arr6[i] << "-";
        cout << endl;
        delete[]arr6; arr6 = nullptr;
    }
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
